package object;

import java.util.List;

import map.Map;
import object.obj3D.enemy.Character;
import system.MyCamera;

public class Collision {
	// ------------------------------------------------------------
	// / Variable declare
	// ------------------------------------------------------------
	public static int	COL_DONT_CARE = 0;
	public static int	COL_NO_COLLISION = COL_DONT_CARE + 1;
	public static int	COL_WITH_TERRAIN = COL_NO_COLLISION + 1;
	public static int	COL_WITH_CHARACTER = COL_WITH_TERRAIN + 1;
	
	private static float	m_distance;
	private static int	m_collisionType = COL_DONT_CARE;
	private static int	m_offset = 2;
	private static float	m_minDistance = MyCamera.getzMin();
	private static float	m_maxDistance = MyCamera.getzMax();
	private static float	m_inc = 0.5f;
	private static boolean isCollision;
	private static boolean CheckCollisionWithTerrain(){
		float alpha = MyCamera.getAlpha();
		float beta = MyCamera.getBeta();
		for(float r = m_minDistance; r <= m_maxDistance && (!isCollision || r < m_distance); r += m_inc)
		{
			float y = (float) (r * Math.cos(Math.toRadians(alpha)));
			float z = (float) (r * Math.sin(Math.toRadians(alpha)
					* Math.cos(Math.toRadians(beta))));
			float x = (float) (r * Math.sin(Math.toRadians(alpha)
					* Math.sin(Math.toRadians(beta))));
			if(y <= Map.GetMapHeightAtPoint((int)x, (int)z)){
				isCollision = true;
				m_collisionType = COL_WITH_TERRAIN;
				m_distance = r;
				return isCollision;
			}
		}
		isCollision = false;
		m_collisionType = COL_NO_COLLISION;
		return isCollision;
	}
	private static boolean CheckCollisionWithEnemy(TransMatrix matrix){
		float alpha = MyCamera.getAlpha();
		float beta = MyCamera.getBeta();
		for(float r = m_minDistance; r <= m_maxDistance && (!isCollision || r < m_distance); r += m_inc)
		{
			float y = (float) (r * Math.cos(Math.toRadians(alpha)));
			float z = (float) (r * Math.sin(Math.toRadians(alpha)
					* Math.cos(Math.toRadians(beta))));
			float x = (float) (r * Math.sin(Math.toRadians(alpha)
					* Math.sin(Math.toRadians(beta))));
			if(matrix.ColliWith(x, y, z)){
				isCollision = true;
				m_collisionType = COL_WITH_CHARACTER;
				m_distance = r;
				return true;
			}
		}
		return false;
	}
	public static int	CheckCollision(List <Character> character){
		CheckCollisionWithTerrain();
		for(int i = 0; i < character.size(); i++){
			if(!isCollision || (isCollision && character.get(i).GetDistance() - m_offset < m_distance)){
				CheckCollisionWithEnemy(character.get(i).getMatrix());
			}
		}
		return m_collisionType;
	}
	// ------------------------------------------------------------
	// / Getter and setter function
	// ------------------------------------------------------------
	public static float getDistance() {
		return m_distance;
	}
	public static int getCollisionType() {
		return m_collisionType;
	}
	public static void setOffset(int m_offset) {
		Collision.m_offset = m_offset;
	}
	public static int getOffset() {
		return m_offset;
	}
	public static boolean isCollision() {
		return isCollision;
	}
}
